package com.acme.task6;

import java.io.*;

public class CommentRemover {

	public static void main(String[] args) {
		char symbol = ' ';
		int symbolInt = 0;
		String fileName = System.getProperty("user.dir")
				+ "\\src\\com\\acme\\task6\\test.txt";
		boolean oneSlash = false;
		boolean twoSlash = false;
		boolean oneAsterisk = false;
		boolean slashAsterisk = false;

		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					new FileInputStream(fileName)));

			while ((symbolInt = br.read()) > 0) {
				symbol = (char) symbolInt;

				if (oneSlash) {
					if (symbol == '/') { // in the double slash comment
						twoSlash = true;
						oneSlash = false;
						continue;
					} else if (symbol == '*') { // in the slash-asterisk comment
						slashAsterisk = true;
						oneSlash = false;
						oneAsterisk = false;
						continue;
					} else { // not a comment, print skipped slash
						System.out.print('/');
						oneSlash = false;
					}
				}

				if (twoSlash) { // in double slash comment we seek end of a row
					if (symbol == '\n') {
						twoSlash = false;
						// oneSlash = false;
						System.out.print(symbol);
					}
					continue;
				}

				if (slashAsterisk) { // in slash-asterisk we seek asterisk
					if (symbol == '*') {
						oneAsterisk = true;
					} else if ((symbol == '/') && oneAsterisk) { // and slash
																	// after
						slashAsterisk = false;
					} else { // if any other symbol cancel question about end of
								// a comment
						oneAsterisk = false;
					}
					continue;
				}

				if (symbol == '/') {
					oneSlash = true;
					continue;
				}

				System.out.print(symbol);
			}
			br.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}
